<?php
/**
* 广告控制器类
* tengjianxing
*/
require('./classes/module/module_ad.php');
class adControl {
	
	private  $ad_m;
	public function __construct() {
	
		$this->ad_m = new adModule();
	}
	
	/**
	* 根据id得到ad信息
	* @param   int        $w_type         1为wondershare网站，2为aimersoft网站，3为anypdftools.com网站，
	* 4为applemacvideo.com网站，5为www.wondershare.jp网站，6为www.wondershare.de网站
	* @param   int         $ad_id
	*
	* @return  array
	*/
	public function  get_ad_id_info($ad_id, $w_type = 0) {
		
		$sql = "SELECT ad_id, ad_remark, ad_size, ad_photo, ad_alt, ad_url, ad_content, ad_name 
				FROM ad 
				WHERE ad_id = $ad_id 
				LIMIT 1";
		$row =  $GLOBALS['db']->getOneRs($sql);
		

		$sql = "(SELECT '' AS product_name ,ad_p.position_name AS title ,ad_p.position_name,ad_p.position_type,ad_d.cat_id,
						ad_d.delivery_id,ad_p.position_id,ad_d.ws_id 
						FROM ad_delivery ad_d
				LEFT JOIN ad_position ad_p ON ad_p.position_id = ad_d.position_id 
				WHERE ad_d.ad_id = '".$row['ad_id']."'  AND  ad_d.cat_id = 0  AND ad_p.position_id = ad_d.position_id 
				) 
				UNION 
				(SELECT  p.product_name , c.cat_title AS title ,ad_p.position_name ,ad_p.position_type,ad_d.cat_id ,ad_d.delivery_id,ad_p.position_id,ad_d.ws_id  
				FROM ad_delivery AS ad_d
				LEFT JOIN ad_position ad_p ON ad_p.position_id = ad_d.position_id 
				LEFT JOIN category c ON ad_d.cat_id = c.cat_id
				LEFT JOIN won_product won_p ON won_p.id = ad_d.ws_id 
				LEFT JOIN product p ON won_p.won_product_id = p.product_id
				WHERE ad_d.ad_id = '".$row['ad_id']."'   AND  ad_d.cat_id != 0  
				) 
				ORDER BY position_name ASC,  position_type ASC ";
		$row['position_size'] = $this->ad_m->get_ad_info($sql);//得到投放位置数组
		$row['position_size'] = empty($row['position_size']) ? array() : $row['position_size'];
		
		$group_position = '';//得到position_id，cat_id，position_type,ws_id三者组合并且以$$分割

		foreach ($row['position_size'] as $key => $value) {
			
			 //以$$来分割position_id，cat_id，position_type组合 因为三者组合唯一
			$group_position .= !empty($value['delivery_id']) ? $value['position_id'].'|'.$value['cat_id'].'|'.$value['position_type'].'|'.$value['ws_id'].'$$' 
															 : 
															 '';
		}
		$row['group_position'] = $group_position;
		
		//以下判断是得到广告投放位置层次关系
		$position_name = '';
		$delivery_content = '';//得到广告投放位置树形结构

		foreach ($row['position_size'] as $key => $value) {
			
			$value['title'] =  ( $value['position_type'] == 8 && $value['cat_id'] == 0 )? 'All' : $value['title'];
			//为产品或者为文章处理
			if ( ( in_array($value['position_type'], array(1, 5)) == 1 && $value['cat_id'] != 0 ) || $value['position_type'] == 8 ) {
				
				if ($value['position_type'] == 5) {//为产品
					
					$value['title'] = $value['product_name'];
				}
				
				if ($position_name == '' || ($position_name != '' && $value['position_name'] != $position_name) ) {
					$delivery_content .= $value['position_name'].chr(13).'&nbsp;&nbsp;&nbsp;--'.$value['title'].chr(13);
					$position_name = $value['position_name'];
				} else  {
					$delivery_content.= '&nbsp;&nbsp;&nbsp;--'.$value['title'].chr(13);
				}
			
			} else {
				$delivery_content .= $value['position_name'].chr(13);
			}
	
		}
		$row['delivery_content'] = $delivery_content;
		return $row;
	}
	
	/**
	* 增加或者修改ad
	*
	* @param   string      $type            操作类型
	* @param   int         $ad_id           留言id
	* @param   string      array     	      
	* @param   text        $content         留言内容
	* @return  int
	*/
	public  function  operate_ad($type, $ad_id = 0, $array = '', $w_type) {
		
		if ( $type == 'add') {
		
			$row  = $GLOBALS['db']->query("call operate_ad('{$array['ad_name']}', '{$array['ad_remark']}', '{$array['ad_size']}',
															'{$array['ad_photo']}', '{$array['ad_alt']}', '{$array['ad_url']}',
															'{$array['ad_content']}', '{$_SESSION['AName']}', {$ad_id}, {$w_type}, @last_id
														);"
										);
			return array( 	'affect_nums' => $this->ad_m->getOne("SELECT  @inset_num "),
							'last_id' =>$this->ad_m->getOne("SELECT  @last_id ")
						);
		
		} elseif ($type == 'edit') {
			$GLOBALS['db']->query("call operate_ad('{$array[ad_name]}', '{$array[ad_remark]}', '{$array[ad_size]}',
													'{$array[ad_photo]}', '{$array[ad_alt]}', '{$array[ad_url]}',
													'{$array[ad_content]}', '{$_SESSION['AName']}', {$ad_id},{$w_type},@last_id
													);"
								);
			return array(	'affect_nums' => $this->ad_m->getOne("SELECT  @inset_num "),
							'last_id' =>$this->ad_m->getOne("SELECT  @last_id ")
						);
		
		}
	
	}

	/**
	* 增加或者修改投放
	*
	* @param   string      $type          操作类型
	* @param   int         $ad_id         留言id
	* @param   array       $array         要增加或者删除的投放
	* @param   int        $w_type         网站类型
	* @return  int
	*/
	public function  operate_ad_delivery($type = '', $ad_id = 0, $array = '', $w_type = 0) {
		
		if ($type == 'old_add') {//为已经存在的广告
			
			$insert_str = '';
			$str = '';
			foreach ($array AS $key => $value) {
				
				$add_array = explode('|', $value);
				$insert_str .=  "{$str}({$ad_id}, {$add_array[0]}, {$add_array[1]}, {$add_array[2]}, {$w_type}, '{$_SESSION['AName']}',
								'{$_SESSION['AName']}', '{$_SERVER['REQUEST_TIME']}', '{$_SERVER['REQUEST_TIME']}', '{$add_array[3]}')";
				$str = ',';									
			}
			
			if ( !empty($insert_str) ) {
				$sql = " INSERT INTO ad_delivery  
						(ad_id, position_id, cat_id, position_type, ad_web, add_user, update_user, add_time, update_time, ws_id) 
						VALUES $insert_str ";
			}
			$this->ad_m->change_info($sql);	 
			
		} elseif ($type == 'del') {

			foreach ($array AS $key => $value) {
				
				$add_array = explode('|', $value);
				$GLOBALS['db']->delete('ad_delivery', "position_id = {$add_array[0]} AND cat_id = {$add_array[1]}
										AND position_type = {$add_array[2]}  AND ad_web = {$w_type}  AND ws_id = {$add_array[3]}"
										);
			}
		
			
		} elseif ($type == 'new_add') { //为木有存在的广告
			
			foreach ($array AS $key => $value) {
				$add_array = explode('|', $value);
				
				$sql  = "SELECT count(*) as nums  
						FROM ad_delivery 
						WHERE   position_id = {$add_array[0]} AND 	cat_id = {$add_array[1]} AND 
								position_type = {$add_array[2]} AND ws_id = {$add_array[3]} AND 
								ad_web = {$w_type}   ";
				
				$nums = $this->ad_m->getOne($sql);
				if ($nums > 0) {
					$data = array('ad_id' => $ad_id);
					$GLOBALS['db']->update('ad_delivery', 
											$data, 
											" position_id = {$add_array[0]} AND cat_id = {$add_array[1]} AND 
											position_type = {$add_array[2]} AND ws_id = {$add_array[3]}  AND 
											ad_web = {$w_type} "
											);
			
				} else {
					$data = array(	'ad_id' => $ad_id,
									'position_id' => $add_array[0],
									'cat_id' => $add_array[1],
									'position_type' => $add_array[2],
									'ad_web' => $w_type,
									'add_user' => $_SESSION['AName'],
									'update_user' => $_SESSION['AName'],
									'add_time' => $_SERVER['REQUEST_TIME'],
									'update_time' => $_SERVER['REQUEST_TIME'],
									'ws_id' => $add_array[3],

								);
					$GLOBALS['db']->insert('ad_delivery', $data); 
				}
			
			}
		}
	
	}
	
	
	/**
	* 得到数据提交条件返回url参数
	* @param   int        $w_type         1为wondershare网站，2为aimersoft网站，3为anypdftools.com网站，4为applemacvideo.com网站，5为www.wondershare.jp网站，6为www.wondershare.de网站
	* @param   int        $ad_location  投放位置
	* @param   string     $ad_size    广告尺寸
	* @param   string     $ad_name    广告名称
	* @return  string
	*/
	public function get_url_par($w_type, $ad_location, $ad_size, $ad_name) { 
		
		$url_str = '&url_page=1';
		if ( !empty($w_type) ) {
			$url_str .= '&w_type='.$w_type;
		}
		
		if( $ad_location != '0|0|0' ) {
			$url_str .= '&ad_location='.$ad_location;
		}
		
		if(!empty($ad_size))
		{
			$url_str.='&ad_size='.$ad_size;
		}
		if ( !empty($ad_name) ) {
			$url_str .= '&ad_name='.$ad_name;
		}
		
		return  $url_str;
	}
	
	/**
	* 得到在搜索广告的条件
	*
	* @param   string        $ad_location   广告投放位置
	* @param   string        $ad_name   广告名称
	* @param   string        $ad_size   广告尺寸
	* @return  array
	*/
	public function get_search_condition($ad_location, $ad_name, $ad_size) {
		
		$where_ad_str = $where_d_str = '';//设置广告条件 和广告投放 条件为空
		$e_location = explode('|', $ad_location);
		$where_ad_str .= empty($ad_name) ? '' : " AND ad.ad_name LIKE  '%{$ad_name}%' ";	 
		$where_ad_str .= empty($ad_size) ? '' : " AND ad.ad_size = '{$ad_size}' ";
			 
		if ($e_location[2] == 1 && $e_location[1] == 0  ) {//代表为文章广告位
			$where_d_str.=' AND position_id = '.$e_location[0];
		} else {
			$where_d_str .= empty($e_location[0]) ? '' : ' AND ad_delivery.position_id = '.$e_location[0];	 
			$where_d_str .=  empty($e_location[1]) ? '' : ' AND ad_delivery.cat_id = '.$e_location[1];	
			$where_d_str .= empty($e_location[2]) ? '' : ' AND ad_delivery.position_type = '.$e_location[2];	
		}
		return array('where_d_str' => $where_d_str, 'where_ad_str' => $where_ad_str);
	
	}
	
	/**
	* 得到分页相关信息
	*
	* @param   int        $page       页码
	* @param   int        $type       类型
	* @param   int        $w_type     网站类型
	* @param   int        $pagesize   取多少条记录
	* @param   int        $ad_location  投放位置
	* @param   string     $ad_size    广告尺寸
	* @param   string     $ad_name    广告名称
	* @return  array
	*/
	public function get_url_info($page, $type, $pagesize = 10, $w_type, $ad_location='', $ad_size = '', $ad_name = '') {
		
		$url_str = '';
		if ($type == 'list_index') {////不带条件搜索
			$sql_p = "	SELECT COUNT(*) AS total 
						FROM ad 
						WHERE   ad.ad_web  = {$w_type}  AND ad.is_text_ad = 0 AND is_delete = 0 ";
		} else {//带条件搜索
			
			$search_info = $this->get_search_condition($ad_location, $ad_name, $ad_size);//得到搜索条件
			
			if ( empty($search_info['where_ad_str']) && empty($search_info['where_d_str']) ) {
				
				$sql_p = "SELECT COUNT(*) AS total 
						FROM ad 
						WHERE   ad.ad_web  = {$w_type} AND   ad.is_delete = 0 AND ad.is_text_ad = 0";
			} elseif ( !empty($search_info['where_ad_str']) && empty($search_info['where_d_str']) )
			{
				$sql_p = "SELECT COUNT(*) AS total .
						FROM ad 
						WHERE   ad.ad_web  = {$w_type} {$search_info['where_ad_str']} AND   ad.is_delete = 0 AND ad.is_text_ad = 0 ";
			} else  {
				$search_info['where_ad_str'] = " ad.ad_web  = {$w_type} {$search_info['where_ad_str']} {$search_info['where_d_str']}";
				$sql_p = "SELECT COUNT(*) AS total 
						FROM ad,ad_delivery  
						WHERE {$search_info['where_ad_str']} AND ad.ad_id = ad_delivery.ad_id AND ad.is_delete = 0  AND ad.is_text_ad = 0  
						GROUP BY ad_delivery.ad_id  ";
			}
			
			$url_str = $this->get_url_par($w_type, $ad_location, $ad_size, $ad_name);
		
		}
		$page_info = get_page_info($sql_p, $pagesize, $page);
		$page_url = get_page_url($page, $page_info['page_num'], $url_str);
		
		return  array('page_info' => $page_info, 'page_url' => $page_url);
	}
	
	/**
	* 得到列表ad信息
	*
	* @param   string     $type       类型（list为列表 不带搜索 而 list_s 在列表 带有搜索）
	* @param   int        $start      从那行开始
	* @param   int        $pagesize   取多少条记录
	* @param   int        $w_type     网站类型
	* @param   int        $ad_location  投放位置
	* @param   string     $ad_size    广告尺寸
	* @param   string     $ad_name    广告名称
	* @return  array
	*/
	public function  get_ad_info($type,$start = 0,$pagesize = 10,$w_type,$ad_location='',$ad_size = '',$ad_name = '') {
		
		$row = $size_row = array();
		
		if ($type == 'list_index') {//不带条件搜索
			
			$sql = "SELECT ad_id, ad_name, ad_remark, ad_url, ad_size, ad_user_update, 
					IF(ad_update > 0,  FROM_UNIXTIME(ad_update, '%Y-%m-%d'), '') ad_update, ad_photo 
					FROM ad 
					WHERE is_text_ad = 0 and ad_web  = {$w_type}  AND is_delete = 0  
					ORDER BY ad_id DESC 
					LIMIT {$start},{$pagesize} ";
			$row = $this->ad_m->get_ad_info($sql);
		} elseif ($type == 'list_search') {//带条件搜索
			
			$search_info = $this->get_search_condition($ad_location, $ad_name, $ad_size);//得到搜索条件
			if ( empty($search_info['where_ad_str']) && empty($search_info['where_d_str']) ) {
				$sql = "SELECT ad_id, ad_name, ad_remark, ad_url, ad_size, ad_user_update,
						IF(ad_update > 0, FROM_UNIXTIME(ad_update, '%Y-%m-%d'), '') ad_update,ad_photo 
						FROM ad 
						WHERE is_text_ad = 0 AND  ad_web  = {$w_type}  AND is_delete = 0  
						ORDER BY ad_id DESC 
						LIMIT {$start},{$pagesize}";
			} else if ( empty($search_info['where_d_str']) && !empty($search_info['where_ad_str']) ) {
				
				$sql = "SELECT ad_id, ad_name, ad_remark, ad_url, ad_size, ad_user_update,
						IF(ad_update > 0,  FROM_UNIXTIME(ad_update, '%Y-%m-%d'), '') ad_update,ad_photo 
						FROM ad 
						WHERE is_text_ad = 0 AND   ad_web  = {$w_type} {$search_info['where_ad_str']}  AND is_delete = 0 
						ORDER BY ad_id DESC 
						LIMIT {$start},{$pagesize} ";
			} else {
				$search_info['where_ad_str'] = '  ad.ad_web  = '.$w_type.$search_info['where_ad_str'].$search_info['where_d_str'];
				$sql = "SELECT ad.ad_id,ad.ad_name,ad.ad_remark,ad.ad_url,ad.ad_size,ad.ad_user_update,
						IF(ad.ad_update > 0,  FROM_UNIXTIME(ad.ad_update, '%Y-%m-%d'), '') ad_update,ad.ad_photo   
						FROM ad,ad_delivery  
						WHERE {$search_info['where_ad_str']} AND  ad.ad_id = ad_delivery.ad_id  AND ad.is_delete = 0 AND ad.is_text_ad = 0   
						GROUP BY ad_delivery.ad_id 
						ORDER BY ad.ad_id DESC
						LIMIT {$start},{$pagesize} ";
			}
			$row = $this->ad_m->get_ad_info($sql);
		}
		
		foreach ($row as $key => $value) {
		
			$sql = "(SELECT ad_position.position_name AS title ,ad_position.position_name, ad_position.position_type, ad_position.position_size, ad_delivery.cat_id 
					FROM ad_delivery,  ad_position
					WHERE ad_delivery.ad_id = {$value['ad_id']}  AND ad_delivery.cat_id = 0 AND ad_position.position_id = ad_delivery.position_id 
					) 
					UNION 
					(SELECT category.cat_title AS title, ad_position.position_name, ad_position.position_type, ad_position.position_size ,ad_delivery.cat_id 
					FROM ad_delivery,ad_position,category  
					WHERE ad_delivery.ad_id  = {$value['ad_id']} AND ad_delivery.cat_id = category.cat_id  AND ad_position.position_id = ad_delivery.position_id 
					) 
					ORDER BY position_name ASC, position_type ASC 
					";
			$row[$key]['position_size'] = $this->ad_m->get_ad_info($sql);
			
			if ( !in_array($value['ad_size'], $size_row)) {
				$size_row[$key] = $value['ad_size'];
			}
		}
		return array('date'=>$row, 'size'=>$size_row);
	}
	
	/**
	* 得到存在于ad_delivery投放表字段
	*
	* @param   string     $find_field     FIND_IN_SET的字段
	* @param   int        $delivery       是否存在
	* @param   string        $select_field  查询的字段
	* @param   string        $where_str   条件
	* @return  string
	*/
	public function  get_delivery_find($find_field = '',$delivery = 0,$select_field = '' , $where_str = '') {
		
		$ad_id_str = '';
		$sql = "SELECT {$select_field}  
				FROM ad_delivery  {$where_str}  
				GROUP BY {$select_field} ";
		$row =  $this->ad_m->get_ad_info($sql);
		
		foreach ($row as $key => $value) {
			
			if ( !empty($value[$select_field]) ) {
			  $ad_id_str .= empty($ad_id_str) ? $value[$select_field] : ','.$value[$select_field];
			}
			
		}
		
		if ($delivery == 1) {
			$where_str= empty($ad_id_str) ? '' : " AND FIND_IN_SET( {$find_field},'{$ad_id_str}')";
		} else {
			$where_str= empty($ad_id_str) ? '' : " AND !FIND_IN_SET( {$find_field},'{$ad_id_str}')";
		}
		
		return $where_str;
	}
	
	/**
	* 得到已经存在的投放位置
	*
	* @param   string        $new_group_position  根据新选择的投放位置数组
	* @param   string        $w_type   网站类型
	* @return  array
	*/
	
	public function get_exist_ad_position($new_group_position = array(), $new_group = array(), $w_type = 1 ) {
		
		$exist_ad_position = '';

		foreach ($new_group_position AS $key => $values) 
		{
			$rows = explode('|', $values);
			
			$sql = "SELECT count(1) AS nums 
					FROM ad_delivery 
					WHERE  position_id = '{$rows[0]}' AND  cat_id = '{$rows[1]}' AND  
							position_type = '{$rows[2]}' AND  ws_id = '{$rows[3]}' AND ad_web = {$w_type}";
			
			if ($GLOBALS['db']->getOne($sql) > 0) {
				 $tips = $new_group[$values][0] . '  '.$new_group[$values][1].chr(13) ;
				$exist_ad_position .= ($exist_ad_position == '') ? $tips : '   '.$tips;
			}
			
		}
		return $exist_ad_position;
	}
	
	
	/**
	* 按照条件得到投放数据
	*
	* @param   string     $where_str     条件
	* @return  array
	*/
	
	public function get_position_info($where_str = '') {
		
		$sql = "SELECT 
				ad_p.position_id, 
				ad_p.position_name, 
				ad_p.position_type,
				ad_d.delivery_id 
				FROM ad_position  AS ad_p LEFT JOIN  ad_delivery  AS ad_d ON ad_d.position_id = ad_p.position_id  
				{$where_str}  
				GROUP BY ad_p.position_id 
				ORDER BY  ad_p.position_type ASC , ad_p.position_id ASC ";
		$row =  $this->ad_m->get_ad_info($sql);
		return $row;
	}
	/**
	* 按照条件得到广告位列表
	*
	* @param   array     $row     一般为投放数据数组
	* @param   string     $where_str     条件
	* @param   string     $type          类型 不同类型 结果不同
	* @param   array     $check_row      已经选择的 
	* @return  array
	*/
	
	public function get_ad_position_list($row, $where_str = '', $type = '', $w_type = 0, $check_row = array() ) {
	
		//以下代码片段是得到广告位列表 分产品 ，文章 ，和其他三种方式
		foreach ($row as $key => $value) {
			
			if (in_array($value['position_type'], array(1, 5, 8)) ) {//当为产品或者文章的时候
				
				$cat_array =  array();
				if ($value['position_type'] == 5) {//表示为产品
					
				    $sql = "SELECT 
				    		cat.cat_id,
				    		cat.cat_title 
				    		FROM category cat  
				    		WHERE  cat.cat_web = $w_type AND cat.cat_type = 2 AND cat.is_delete = 0 AND cat.cat_series = 1  
				    		GROUP  BY  cat.cat_id ";//得到某个网站的一级分类名称
					$cat_array =  $this->ad_m->get_ad_info($sql);
					
					//以下是根据一级分类 得到相应的产品
				    foreach ($cat_array as $cat_key => $cat_value) {
				    	
				        $cat_array[$cat_key]['cat_box'] =  '<input type="checkbox" name="delivery_list"   value=""  
				        									onclick="set_checked(\'p_cat_'.$value['position_id'].'_'.$cat_value['cat_id'].'\')"  
				        									id="p_cat_'.$value['position_id'].'_'.$cat_value['cat_id'].'" >';
				        
				        $sql = "SELECT cat_id 
				        		FROM category 
				        		WHERE   FIND_IN_SET({$cat_value['cat_id']}, parent_path) AND cat_web = {$w_type}
				        				AND cat_type =2 AND is_delete = 0 AND cat_series = 3 ";
				        
						$sql = "SELECT * FROM 
								( 
									SELECT  ad.ad_name, won_p.id, p.product_name, ad_d.delivery_id 
									FROM won_product won_p 
									LEFT JOIN product p ON p.product_id = won_p.won_product_id 
									LEFT JOIN ad_delivery   ad_d ON ad_d.ws_id = won_p.id  
									LEFT JOIN ad   ON ad_d.ad_id = ad.ad_id  
									WHERE  won_p.cat_id IN ({$sql}) AND (ad_d.is_text_ad IS NULL OR ad_d.is_text_ad = 0 )  
									AND  ad_d.position_id = {$value['position_id']}   AND won_p.won_product_delete = 0 
									UNION 
									SELECT '' AS ad_name,  won_p.id, p.product_name, ad_d.delivery_id 
									FROM won_product won_p 
									LEFT JOIN product p ON p.product_id = won_p.won_product_id 
									LEFT JOIN ad_delivery   ad_d ON ad_d.ws_id = won_p.id  
									LEFT JOIN ad   ON ad_d.ad_id = ad.ad_id  
									WHERE  	won_p.cat_id IN ({$sql}) AND (ad_d.position_id IS NULL OR ad_d.position_id != {$value['position_id']} )  AND 
											won_p.won_product_delete = 0 
								) a  ";
					
						
						$product_array = $this->ad_m->get_ad_info($sql);
						
						foreach ($product_array as $p_key => $p_value) {
							
							$check_str = '';
							
					        //是否存在已经有的投放数组
					        if ( in_array("{$value['position_id']}|{$cat_value['cat_id']}|{$value['position_type']}|{$p_value['id']}", $check_row) ) {
					        	
					            $check_str = 'checked="checked"';
					        }
					        $checkbox_value = "{$p_value['product_name']}&&%{$p_value['ad_name']}&&%{$value['position_id']}|{$cat_value['cat_id']}|{$value['position_type']}|{$p_value['id']}";
						 	$product_array[$p_key]['p_box'] =  "<input type='checkbox' name='delivery_list'   id='p_cat_{$value['position_id']}_{$cat_value['cat_id']}_{$p_value['id']}' 
						 										{$check_str} value='{$checkbox_value}'>";
						}
						
						$cat_array[$cat_key]['product'] = $product_array;
						
				    }
				    unset($product_array);
				} elseif ( $value['position_type'] == 1)  {//表示为文章 
					
					$checkbox_prefix = 'a_cat_';
					
				    $sql = "SELECT * FROM ( 
					    		SELECT ad.ad_name, cat.cat_id, cat.cat_title, ad_d.delivery_id, ad_d.ws_id  
					    		FROM category cat 
					    		LEFT JOIN  ad_delivery  AS ad_d ON ad_d.cat_id = cat.cat_id 
					    		LEFT JOIN ad ON  ad_d.ad_id = ad.ad_id  
				    			$where_str AND (ad_d.is_text_ad IS NULL OR ad_d.is_text_ad = 0 )  AND  ad_d.position_id = ".$value['position_id']." 
					    		UNION 
					    		SELECT '' AS ad_name,cat.cat_id,cat.cat_title,ad_d.delivery_id,ad_d.ws_id  
					    		FROM category cat 
					    		LEFT JOIN  ad_delivery  AS ad_d ON ad_d.cat_id = cat.cat_id 
					    		LEFT JOIN ad ON  ad_d.ad_id = ad.ad_id  
				    			$where_str  AND  (ad_d.position_id IS NULL OR ad_d.position_id != ".$value['position_id']." ) 

				    		) a 
				    		GROUP BY cat_id ";//得到某个网站的三级分类名称
					$cat_array =  $this->ad_m->get_ad_info($sql);
					
				    foreach ($cat_array as $ad_key => $ad_value) {
				    	
				 
				        $check_str = '';
				        //是否存在已经有的投放数组
				        if( in_array("{$value['position_id']}|{$ad_value['cat_id']}|{$value['position_type']}|0", $check_row)) {
				            $check_str = 'checked="checked"';
				        }
				        $checkbox_value = "{$ad_value['cat_title']}&&%{$ad_value['ad_name']}&&%{$value['position_id']}|{$ad_value['cat_id']}|{$value['position_type']}|0";
				        $cat_array[$ad_key]['checkbox'] =  "<input type='checkbox' name='delivery_list'  
				       										id='{$checkbox_prefix}{$value['position_id']}_{$ad_value['cat_id']}' {$check_str} value='{$checkbox_value}'>";
				
				    }
				
			}  elseif ( $value['position_type'] == 8 )  {//表示为文章 或 store分类广告位
					
					
					$checkbox_prefix = 'store_cat_';

					
					$sql = "SELECT 0 AS  cat_id, 'All' AS cat_title 
							UNION ALL
							SELECT cat_id, cat_title 
							FROM category 
							WHERE cat_web = {$w_type} AND cat_type = 2  AND is_delete = 0 AND cat_series = 1 
							";

				    $sql = "SELECT * FROM ( 
					    		SELECT ad.ad_name, cat.cat_id, cat.cat_title, ad_d.delivery_id, ad_d.ws_id  
					    		FROM ({$sql}) cat
					    		LEFT JOIN  ad_delivery  AS ad_d ON ad_d.cat_id = cat.cat_id 
					    		LEFT JOIN ad ON  ad_d.ad_id = ad.ad_id  
					    		WHERE ad_d.is_text_ad IS NULL OR ad_d.is_text_ad = 0 AND  ad_d.position_id = {$value['position_id']}
					    		UNION 
					    		SELECT '' AS ad_name, cat.cat_id, cat.cat_title, ad_d.delivery_id, ad_d.ws_id  
					    		FROM ({$sql}) cat
					    		LEFT JOIN  ad_delivery  AS ad_d ON ad_d.cat_id = cat.cat_id 
					    		LEFT JOIN ad ON  ad_d.ad_id = ad.ad_id  
					    		WHERE  ad_d.position_id IS NULL OR ad_d.position_id != {$value['position_id']} 
				
				    		) a 
				    		GROUP BY cat_id ";//得到某个网站的一级级分类名称
	
					$cat_array =  $this->ad_m->get_ad_info($sql);
					
				    foreach ($cat_array as $ad_key => $ad_value) {
				    	
				 
				        $check_str = '';
				        //是否存在已经有的投放数组
				        if( in_array("{$value['position_id']}|{$ad_value['cat_id']}|{$value['position_type']}|0", $check_row)) {
				            $check_str = 'checked="checked"';
				        }
				        $checkbox_value = "{$ad_value['cat_title']}&&%{$ad_value['ad_name']}&&%{$value['position_id']}|{$ad_value['cat_id']}|{$value['position_type']}|0";
				        $cat_array[$ad_key]['checkbox'] =  "<input type='checkbox' name='delivery_list'  
				       										id='{$checkbox_prefix}{$value['position_id']}_{$ad_value['cat_id']}' {$check_str} value='{$checkbox_value}'>";
				
				    }
				}
				$row[$key]['cat_info'] = $cat_array;
				
			} else {//为其他
				
				if ($type != 'ad_list') {//不等于列表显示，只用于广告编辑
					
				    $check_str = '';
				    //是否存在已经有的投放数组
				    if (in_array("{$row[$key]['position_id']}|0|{$row[$key]['position_type']}|0",$check_row))  {
				        $check_str = 'checked="checked"';
				    }
				    
				    $checkbox_value = "{$row[$key]['position_name']}&&%{$row[$key]['ad_name']}&&%{$row[$key]['position_id']}|0|{$row[$key]['position_type']}|0";
				    $row[$key]['checkbox'] = "<input type='checkbox' name='delivery_list' id='delivery_id{$row[$key]['position_id']}' {$check_str}
				   							 value='{$checkbox_value}'>";
				}
			}
		
		}
		return  $row;
	}
	
	/**
	* 获得某个网站的所有广告投放位置
	* @param   int        $w_type     网站类型
	* @param   int        $type       类型
	* @param   array      $check_row  已经选择的投放位置
	* @param   array      $search_array  搜索
	* @return  array
	*/
	public function  get_delivery_size_info($w_type = 0, $type = '', $check_row = array(), $search_array = array() ) {
		
		$position_info = $this->get_position_info(" WHERE ad_p.ad_web = {$w_type}  AND ad_p.is_delete = 0");
		//得到广告位 以及广告位上的 广告
		$row = $this->get_ad_position_list(	$position_info,
											" WHERE cat.cat_web = {$w_type} AND cat.cat_type = 1  AND cat.is_delete = 0 AND 
											cat.cat_series = 3 ",
											$type, $w_type, $check_row
											);
		$sql = "SELECT 
				ad_id, 
				ad_size 
				FROM  ad 
				WHERE ad_web = {$w_type} AND is_delete = 0 AND ad_size IS NOT NULL  
				GROUP BY ad_size ";

		return  array('date' => $row, 'size' => $this->ad_m->get_ad_info($sql));
	}


}





?>